<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="1" formType="add" labelWidth="100" :isSubmitBtn="false" @formChange="formChangeHandle" />

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="saveDialog">保存</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { modifyCareTaskEmp, getUserInfoByRole, getUserInfo } from '@/api/customerRepair/visitRules/careTaskSearch2';
export default {
  name: 'redistributionCare',
  components: {},
  mixins: [dictionary],
  props: {
    careTaskIds: Object
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getUserInfo().then(res => {
        if (res.resultCode == 200) {
          let param = {
            companyId: res.data.userInfo.companyId,
            positionCode: 10061015
          };
          getUserInfoByRole(param).then(res => {
            if (res.resultCode == 200) {
              this.formList[0].itemList = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
            } else {
              this.$message.error('获取关爱人员列表失败!');
            }
          });
        } else {
          this.$message.error('获取当前登录用户信息失败!');
        }
      });
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '关爱人员',
          fieldName: 'employeeId',
          placeholder: '关爱人员',
          initialValue: '',
          itemList: []
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    closeDialog(refresh) {
      if (refresh) {
        this.$emit('changeHandle', { dataSources: 10451001 });
      }
      this.$emit('close', false);
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (!this.careTaskIds || this.careTaskIds.length <= 0) {
        this.$message.error('请先选择关爱任务!');
        return;
      }

      if (this.saveData !== null) {
        this.saveData.careTaskIds = this.careTaskIds;
        let res = await modifyCareTaskEmp(this.saveData);
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        } else {
          this.$message(`${res.errMsg}`);
        }
      }
    }
  }
};
</script>
